#!/usr/bin/env python3

with open('input.txt') as file:
    could_be_in = {}
    for line in file.read().splitlines():
        # pmctk hnjj drtg (contains nuts, fish)
        ingredients, allergens = line.split(" (contains ")
        ingredients = set(ingredients.split())
        allergens = allergens[:-1].split(", ")

        for a in allergens:
            if a in could_be_in:
                could_be_in[a] &= ingredients
            else:
                could_be_in[a] = set(ingredients)

    mapping = []
    while could_be_in:
        new = []
        for a, ii in could_be_in.items():
            if len(ii) == 1:
                new.append((a, ii.pop()))

        for a, _ in new:
            del could_be_in[a]

        for _, i in new:
            for v in could_be_in.values():
                if i in v:
                    v.remove(i)

        mapping += new

        print(mapping)

    mapping.sort()
    print(",".join(i for _, i in mapping))